package com.gxwz.medical.domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxwz.medical.database.DbUtil;
import com.gxwz.medical.exception.DbException;

/**
 * 农合机构管理类
 * @author 演示
 *
 */
public class Institution {
	
	/**
	 * 行政区域编码，类标识符
	 */
	private Area area;
	/**
	 * 经办机构编码
	 */
	private String agencode;
	/**
	 * 经办机构名称
	 */
	private String agenname;
	/**
	 * 经办机构级别，区别于行政区域级别，1表示县级农合办，2表示镇级农合点
	 */
	private int grade;
	
	private String areacode;
	
	
	public String getAreacode() {
		return areacode;
	}
	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}
	public Institution(){
		
	}
	public Institution(Area area, String agencode, String agenname, int grade) {
		super();
		this.area = area;
		this.agencode = agencode;
		this.agenname = agenname;
		this.grade = grade;
	}
	/**
	 * 实例化对象
	 * @param areacode
	 * @throws DbException
	 */
	public Institution(Area area)throws DbException{
		  if(area==null){
			  throw new IllegalStateException("行政区域编码不能空");
		  }
		try{
			this.area=area;
			loadDB();
	    }catch(SQLException e){
	    	throw new DbException("加载id="+this.area.getAreacode()+"农合经办机构记录失败", e);
	    }
	}
	
	
	/**
	 * 
	 * @param areaid
	 * @throws SQLException
	 */
	private void loadDB()throws SQLException{
		Connection conn =null;
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		try{
			conn=DbUtil.getConn();
			pstmt=conn.prepareStatement("select * from t_institution where areacode=?");
			int index=1;
			pstmt.setString(index, this.area.getAreacode());
			rs=pstmt.executeQuery();
			if(rs.next()){
				this.area=new Area(rs.getString("areacode"));
				this.agencode=rs.getString("agencode");
				this.agenname=rs.getString("agenname");
				this.grade=rs.getByte("grade");
			}
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(rs, pstmt, conn);
		}
	}
	/**
	 * 添加农合经办点
	 * @param areacode
	 * @param agencode
	 * @param agenname
	 * @param grade
	 */
	public void addInst(Area area,String agencode,String agenname,int grade)throws Exception{
		Connection conn=null;
		try{
			//初始化属性
			this.area=area;
			this.agencode=agencode;
			this.agenname=agenname;
			this.grade =grade;
		   //保存导数据库
			conn=DbUtil.getConn();
			conn.setAutoCommit(false);
			//调用插入数据库私有方法
			this.saveToDB(conn);
			conn.commit();
		}catch(Exception e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
		
	}
	/**
	 * 插入记录
	 * @param connection
	 * @throws SQLException
	 */
	private void saveToDB(Connection connection)throws SQLException{
		PreparedStatement pstmt=null;
		try{

			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into t_institution(areacode,agencode,agenname,grade)");
			sqlBuff.append("values(?,?,?,?)");
			String sql=sqlBuff.toString();
			pstmt=connection.prepareStatement(sql);
			//依次设置动态参数的值，注意顺序
			int index=1;
			pstmt.setString(index++, this.area.getAreacode());//行政区域编码
			pstmt.setString(index++, this.agencode);
			pstmt.setString(index++, this.agenname);
			pstmt.setInt(index, this.grade);
			//以executeUpdate()执行SQL
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw e;
		}finally{
			//只关闭PrepareStatement资源
			if(pstmt!=null){
				pstmt.close();
			}
			
		}
		
	}

	
	public void edit(Area area,String agencode,String agenname,int grade)throws SQLException{
		Connection conn =DbUtil.getConn();
		try{
			   this.area=area;
			   this.agencode=agencode;
			   this.agenname=agenname;
			   this.grade=grade;
			   conn.setAutoCommit(false);
			   updateToDB(conn);
			   conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	private void updateToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造update SQL语句字符串变量sql
			String  sql="update t_institution t set t.areacode=?,t.agenname=?,t.grade=? where t.agencode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt=conn.prepareStatement(sql.toString());
			 //设置动态参数对应的值
			
	/*		int index=1;
			pstmt.setString(index++, this.area.getAreacode());//行政区域编码
			pstmt.setString(index++, this.agencode);
			pstmt.setString(index++, this.agenname);
			pstmt.setInt(index++, this.grade);
	*/
			 pstmt.setString(1, this.area.getAreacode());//行政区域编码
			 pstmt.setString(2, this.agenname);
			 pstmt.setInt(3, this.grade);
			 pstmt.setString(4, this.agencode);
			//以executeUpdate()执行SQL
			pstmt.executeUpdate();
			System.out.println(  pstmt.executeUpdate());
		}catch(SQLException e){
			  throw e;
		  }finally{
			  DbUtil.close(pstmt);
		  } 
	}
	
	public void del(String areacode)throws SQLException{

		Connection conn=null;
 	   //1:对象属性赋值
		this.areacode=areacode;
       try{
    	   conn=DbUtil.getConn();
    	   //2；调用delFromDB方法
    	    delFromDB(conn);
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DbUtil.close(conn);
       }
	}
	private void delFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			//定义构造插入SQL语句字符串变量sql
			String  sql="delete from t_institution where areacode=?";
			 //创建执行带动态参数的SQL的PreparedStatement pstmt
			pstmt=conn.prepareStatement(sql); 
			int index=1;
			 pstmt.setString(index++, this.getAreacode());
		
			int count=pstmt.executeUpdate();
			
		}catch(SQLException e){
			throw e;
		}finally{
			DbUtil.close(pstmt);
			
		}
	}
	
	
	public String getAgencode() {
		return agencode;
	}
	public void setAgencode(String agencode) {
		this.agencode = agencode;
	}
	public String getAgenname() {
		return agenname;
	}
	public void setAgenname(String agenname) {
		this.agenname = agenname;
	}
	public int getGrade() {
		return grade;
	}
	public void setGrade(int grade) {
		this.grade = grade;
	}
	public Area getArea() {
		return area;
	}
	public void setArea(Area area) {
		this.area = area;
	}


}
